package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Reserve;
import com.baizhi.springboot.entity.User;
import com.baizhi.springboot.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ReserveController {
    @Autowired
    private ReserveService reserveService;

    @RequestMapping("/selectReserveByPage")
    public Map selectReserveByPage(Integer page,Integer limit,Reserve reserve) {
        System.out.println(reserve);
        return reserveService.selectReserveByPage(page,limit,reserve);
    }
    @RequestMapping("/insertReserve")
    public Map insertReserve(Reserve reserve,HttpSession session) {
        User user = (User) session.getAttribute("user");
        HashMap<Object, Object> map = new HashMap<>();
        try {
            //reserve.setRecord(user.getName());
            reserve.setReservedate(new Date());
            reserveService.insertReserve(reserve);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteReserve")
    public Map  deleteReserve(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            reserveService.deleteReserve(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneReserve")
    public Map selectOneReserve(Integer id, HttpSession session) {
        Reserve reserve= reserveService.selectOneReserve(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("reserve", reserve);
        return map;
    }

    @RequestMapping("/updateReserve")
    public Map updateReserve(Reserve reserve) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            //System.out.println(label);
            reserveService.updateReserve(reserve);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
